package com.yin.lock8;

import java.util.concurrent.TimeUnit;

/**
 * 8lock
 * 增加了一个普通方法之后，先执行普通方法
 */
public class Test5 {
    public static void main(String[] args) {
        Phone5 phone = new Phone5();
        new Thread(()->{
            phone.sendMsg();
        },"A").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            phone.call();
        },"B").start();
    }

}

class Phone5 {

    // 加了static, 类一加载就有了, synchronized锁的是类的class模板
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMsg");
    }

    // 锁的是生成的对象，调用者
    public synchronized void call() {
        System.out.println("calling");
    }
}
